package com.tanhua.dubbo.api;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.api.mapper.UserInfoMapper;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户服务接口实现类
 */
@DubboService
public class UserInfoApiImpl implements UserInfoApi{

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 完善个人信息服务方法
     *
     * @param userInfo
     */
    @Override
    public void add(UserInfo userInfo) {

        userInfoMapper.insert(userInfo);
    }

    @Override
    public List<UserInfo> findByBatchIds(List<Long> listBlackUserId) {
        return userInfoMapper.selectBatchIds(listBlackUserId);
    }

    /**
     * 更新用户信息
     * @param userInfo
     */
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 用户资料读取
     * @param currentUserId
     * @return
     */
    @Override
    public UserInfo findUserInfo(Long currentUserId) {
        return userInfoMapper.selectById(currentUserId);
    }

    /**
     * 根据推荐用户id以及条件进行过滤用户数据
     *
     * @param serviceUserIds
     * @param condition
     * @return
     */
    @Override
    public Map<Long, UserInfo> findByCondition(List<Long> serviceUserIds, UserInfo condition) {
        //1.构造查询tb_userInfo表条件对象
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        if(condition != null) {
            Integer age = condition.getAge();
            String gender = condition.getGender();
            String nickname = condition.getNickname();
            if(age != null){ ///age <= 30
                queryWrapper.le("age",age);
            }
            if(StringUtils.isNotEmpty(gender)){
                queryWrapper.eq("gender",gender);
            }
            if(StringUtils.isNotEmpty(nickname)){
                queryWrapper.like("nickname",nickname);
            }
        }
        //1.1 加上serviceUserIds
        queryWrapper.in("id",serviceUserIds);
        //2.执行查询tb_userInfo得到List<userInfo>
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        //3.将List<userInfo>转换为Map<Long, UserInfo>(为了业务使用的时候更方便)
        //3.1定义Map<Long, UserInfo> 返回数据
        Map<Long, UserInfo> userInfoMap = new HashMap<>();
        if(!CollectionUtils.isEmpty(userInfoList)){
            //方式一：
            for (UserInfo userInfo : userInfoList) {
                userInfoMap.put(userInfo.getId(),userInfo);
            }
            //方式二：
            //userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, b -> b));
        }
        return userInfoMap;
    }

    @Override
    public PageResult findPage(Long page, Long pageSize) {

        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(new Page<>(page, pageSize), null);
        // 构建分页结果返回
        return new PageResult(page,pageSize,userInfoPage.getTotal(), userInfoPage.getRecords());
    }




}